<?php

Raise::load('core.RaiseObject');
Raise::load('core.datetime.RaiseDateTime');

/**
 * RaiseFunction class
 * Function related functionality
 *
 * @author Sam-Mauris Yong / hellclanner at live dot com
 * @license http://www.opensource.org/licenses/bsd-license New BSD License
 * @package phpraise.core.reflection
 * @since 1.2
 */
class RaiseObjectDumper extends RaiseObject{
    
    /**
     * The object
     * @var object
     */
    private $object;
    
    /**
     * Create a new RaiseObjectDumper
     * @param object $object 
     */
    public function __construct($object){
        $this->object = $object;
    }
    
    /**
     * Get the object that this dumper is handling
     * @return object
     */
    public function object(){
        return $this->object;        
    }
    
    /**
     * Dump information about this object to the dump file in the temporary folder
     */
    public function dump($note = ''){ 
       $schema = 'http://phpraise.googlecode.com/svn/trunk/xml-schema/object-schema.xsd';
       $namespace = 'http://phpraise.googlecode.com/svn/trunk/xml-schema/';
       
       $template = "<?xml version=\"1.0\" encoding=\"UTF-8\" >\n";
       $template .= "<!-- Generator: RaiseObjectDumper; Generated: " . RaiseDateTime::now()->toISO8601() . " -->\n";
       $template .= "<object xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"" . $namespace . "\" xsi:schemaLocation=\"" . $namespace . " " . $schema . "\">\n";
       $template .= "@note";
       $template .= "\t<class><![CDATA[@class]]></class>\n\t<extends><![CDATA[@extends]]></extends>\n\t<implements>@implements</implements>\n";
       $template .= "\t<properties>@props</properties>\n";
       $template .= "</object>";
       
       $impl  = '';
       $a = class_implements($this->object);
       foreach($a as $v){
              $impl .= "\t\t<implement><![CDATA[" . htmlentities($v) . "]]></implement>\n";
       }
       if($impl != ''){
           $impl = "\n" . $impl;
       }
       
       $props = '';
       $a = array();
       if($this->object instanceof RaiseObject){
           $a = $this->object->properties();
       }else{
           $a = get_object_vars($this->object);
       }
       foreach($a as $k => &$v){
           if(is_object($v)){
               $v = get_class($v) . '(Hash ' . spl_object_hash($v) . ')';
           }else if(is_resource($v)){
               $v = get_resource_type($v) . '(ID #' . intval($v) . ')';
           }else if(is_array($v)){
               $tmp = 'array ({' . "\n";
               foreach($v as $x => $y){
                   if(is_object($y)){
                       $tmp .= $x . ': '. get_class($y) . '(Hash ' . spl_object_hash($y) . ')' . "\n";
                   }elseif(is_resource($y)){
                       $tmp .= $x . ': ' . get_resource_type($y) . '(ID #' . intval($y) . ')' . "\n";
                   }elseif(is_array($y)){
                       $tmp .= $x . ': array(' . count($y) . ' items)';
                   }else{
                       $tmp .= $x . ': ' . (string)$y . "\n";
                   }
               }
               $tmp .= '})';
               $v = $tmp;
           }
           $props .= "\t\t<property>\n\t\t\t<name><![CDATA[" . $k . "]]></name>\n\t\t\t<value><![CDATA[" . htmlentities($v) . "]]></value>\n\t\t</property>\n";
       }
       if($props != ''){
           $props = "\n" . $props;
       }
       
       if($note != ''){
           $note = "<!-- \n" . $note . "\n -->\n";
       }
       
       $className = get_class($this->object);
       $output = str_replace(array('@class', '@extends', '@implements', '@props', '@note'), array($className, get_parent_class($this->object), $impl, $props, htmlentities($note)), $template);
       $dumpFile = new RaiseFile(RaisePath::combine(RaisePath::tempPath(), $className . '-' . RaiseDateTime::now()->toFormat('Ymd-His') . '-dump.txt'));
       $dumpFile->write($output);
    }
    
}
